package src.com.qf.controller;


import src.com.qf.entity.User;
import src.com.qf.service.UserService;
import src.com.qf.service.impl.UserServiceImpl;
import src.com.qf.utils.Base64Util;
import src.com.qf.utils.CodeUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "UserController",value = "/user")
public class UserController extends BaseController {
    private UserService userService = new UserServiceImpl();

    /**
     * 注册
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    public String userRegister(HttpServletRequest request, HttpServletResponse response) throws IOException {
        /**
         * 获取参数
         */
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String repassword = request.getParameter("repassword");
        String email = request.getParameter("email");
        String gender = request.getParameter("gender");

        /**
         * 校验
         */
        if (username == null || "".equals(username)){
            request.setAttribute("msg","用户名为空,注册失败");
            return "forward:error/error.jsp";
        }
        if (password == null || "".equals(password) ||!(password.equals(repassword))){
            request.setAttribute("msg","密码输入有误,注册失败");
            return "forward:error/error.jsp";
        }if (email == null || "".equals(email)){
            request.setAttribute("msg","邮箱输入有误,注册失败");
            return "forward:error/error.jsp";
        }

        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setEmail(email);
        user.setGender(Integer.parseInt(gender));
        user.setRole(1);//默认是普通用户
        user.setStatus(0);//默认为未激活状态
        user.setActive_code(CodeUtil.getUUIDCode());
        int result = userService.register(user);

        if (result == 0){
            request.setAttribute("msg","注册失败");
            return "forward:error/error.jsp";
        }else {
            System.out.println("注册成功");
            return "redirect:registerSuccess.jsp";
        }
    }

    /**
     * 校验用户名是否存在
     * @param request
     * @param response
     */
    public String checkUserName(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String username = request.getParameter("username");
        int result = userService.checkUserName(username);
        return String.valueOf(result);
    }

    /**
     * 激活用户的方法
     * @param request
     * @param response
     * @return
     */
    public String active(HttpServletRequest request, HttpServletResponse response){
        String activeCode = request.getParameter("code");
        int result = userService.active(activeCode);
        if (result == 0){
            request.setAttribute("msg","激活失败");
            return "forward:error/error.jsp";
        }else {
            System.out.println("激活成功");
            return "redirect:login.jsp";
        }
    }

    /**
     * 校验验证码
     * @param request
     * @param response
     * @return
     */
    public String checkCode(HttpServletRequest request, HttpServletResponse response){
        String code = request.getParameter("code");
        String sessionCode = (String) request.getSession().getAttribute("sessionCode");
        if (code.equalsIgnoreCase(sessionCode)){
            return "0";
        }else {
            return "";
        }
    }

    /**
     * 登录的方法
     * @param request
     * @param response
     * @return
     */
    public String userLogin(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String code = request.getParameter("code");
        String auto = request.getParameter("auto");
        //校验
        if(username == null || "".equals(username)){
            request.setAttribute("msg","用户名为空,登录失败");
            return "forward:error/error.jsp";
        }
        if(password == null || "".equals(password)){
            request.setAttribute("msg","密码不能为空，登录失败");
            return "forward:error/error.jsp";
        }
        String sessionCode = (String) request.getSession().getAttribute("sessionCode");
        if (!code.equalsIgnoreCase(sessionCode)){
            request.setAttribute("msg","验证码不正确");
            return "forward:error/error.jsp";
        }

        User user = userService.login(username,password);
        if (user==null){
            request.setAttribute("msg","密码不正确，登录失败");
            return "forward:login.jsp";
        }

        //状态持久化
        request.getSession().setAttribute("loginUser",user);

        //自动登录
        if (auto==null){
            //不愿自动登录
            Cookie cookie = new Cookie("autoLogin","");
            cookie.setMaxAge(-1);//失效
            response.addCookie(cookie);
        }else {
            //两周免密登录,明文存储不安全，需进行加密
            String str = username+":"+password;
            String encode = Base64Util.encode(str);
            Cookie cookie = new Cookie("autoLogin",encode);
            cookie.setMaxAge(60*60*24*14);//有效时长2周
            cookie.setPath("/");
            response.addCookie(cookie);

        }

        System.out.println("登陆成功");
        return "redirect:good?action=getGoodsListByTypeId";
    }

    /**
     * 登出的操作
     * @param request
     * @param response
     * @return
     */
    public String logOut(HttpServletRequest request, HttpServletResponse response){
        //清空session域,重定向good?action=getGoodsListByTypeId
        request.getSession().invalidate();
        Cookie cookie = new Cookie("autoLogin","");
        cookie.setMaxAge(0);
        response.addCookie(cookie);
        return "redirect:good?action=getGoodsListByTypeId";
    }
}
